package com.edu.app.controller.linkcontrollers;

import com.edu.app.bean.core.SysCustomerTrace;
import com.edu.app.constant.ErrorConstant;
import com.edu.app.constant.StaticConstant;
import com.edu.app.constant.SysMessage;
import com.edu.app.service.sys.linkservices.SysCustomerTraceService;
import com.edu.app.util.ErrorUtil;
import com.edu.app.util.JWTUtil;
import com.edu.app.util.MsgUtil;

import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping(value = "/api/customertrace")
@RestController
public class SysCustomerTraceController {

    @Autowired
    private SysCustomerTraceService sysCustomerTraceService;


    @RequestMapping(value = "")
    @RequiresAuthentication
    public ModelMap getCustomerTraceList (){

        try{
            List<SysCustomerTrace> customerTraceList = sysCustomerTraceService.getObjectList();
            return MsgUtil.retMsg(SysMessage.GET_CUSTOMERTRACE_SUCCEED, StaticConstant.CUSTOMER_TRACE_LIST, customerTraceList);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @GetMapping(value = "/{id}")
    @RequiresAuthentication
    public ModelMap getCustomerTraceById(@PathVariable Integer id){

        try{
            SysCustomerTrace sysCustomerTrace  = sysCustomerTraceService.getObjectById(id);
            return MsgUtil.retMsg(SysMessage.GET_CUSTOMERTRACE_SUCCEED, StaticConstant.CONTRACT, sysCustomerTrace);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @GetMapping("/pages")
    @RequiresAuthentication
    public ModelMap getCustomerTraceByPage(@RequestParam Integer offset, @RequestParam Integer limit) {

        try {
            PageInfo pageInfo = sysCustomerTraceService.findItemByPage(offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_CUSTOMERTRACE_SUCCEED, "page_info", pageInfo);
        } catch (Exception e) {
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }

    @GetMapping("/pages/{id}")
    @RequiresAuthentication
    public ModelMap getCustomerTraceByUserId(@PathVariable Integer id, @RequestParam Integer offset, @RequestParam Integer limit) {

        try {
            PageInfo pageInfo = sysCustomerTraceService.getCustomerTraceByUserId(id, offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_CUSTOMERTRACE_SUCCEED, "page_info", pageInfo);
        } catch (Exception e) {
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }


    @Transactional
    @PostMapping(value = "")
    @RequiresAuthentication
    public ModelMap addCustomerTrace(@RequestHeader("Authorization") String token,
                                     @RequestParam String content,
                                     @RequestParam Integer customerId,
                                     @RequestParam Integer file_id)
    {

        try{
            String username = JWTUtil.getUsername(token);
            SysCustomerTrace sysCustomerTrace = new SysCustomerTrace();
            sysCustomerTrace.initializeSystemColumns(username);
            sysCustomerTrace.setContent(content);
            sysCustomerTrace.setCustomer_id(customerId);
            sysCustomerTrace.setFile_id(file_id);
            Integer r = sysCustomerTraceService.add(sysCustomerTrace);

            if(r > 0){
                return MsgUtil.retMsg(SysMessage.ADD_CUSTOMERTRACE_SUCCEED, "trace",sysCustomerTrace);
            }else{
                return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
            }
        }catch (org.springframework.dao.DuplicateKeyException e){
            return ErrorUtil.retErrMsg(ErrorConstant.CUSTOMERTRACE_ALREADY_EXISTS);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }


    @Transactional
    @PutMapping(value = "")
    @RequiresAuthentication
    public ModelMap updateCustomerTrace (@RequestHeader("Authorization") String token,
                                         @RequestBody SysCustomerTrace sysCustomerTrace,
                                         @RequestParam Integer lessonId,
                                         @RequestParam Integer points
    ){

        String username = JWTUtil.getUsername(token);
        sysCustomerTrace.initializeSystemColumns(username);

        Integer ret = sysCustomerTraceService.update(sysCustomerTrace);

        if(ret > 0 ){

            return MsgUtil.retMsg(SysMessage.UPDATE_CUSTOMERTRACE_SUCCEED, null, null);
        }
        else{
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }

    }

    @Transactional
    @DeleteMapping(value = "")
    @RequiresAuthentication
    public ModelMap deleteCustomerTrace (@RequestParam Integer id){
        Integer ret;

        ret = sysCustomerTraceService.delete(id);

        if( ret > 0)
        {
            return MsgUtil.retMsg(SysMessage.DELETE_CUSTOMERTRACE_SUCCEED, null, null);
        }
        else
        {
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }

    }
}
